﻿document.observe("dom:loaded", loadMainDealComments);

//chú ý khi upload file.
//các function sử dụng biến strUrlLink: showCommentsByDeal(); showSubCommentSPResult(); 
//var strUrlLink = "http://pcw028/NhomMua_new/"; 

function loadMainDealComments() {
    
    //check trạng thái đăng nhập
    try { checkSession(); } catch (e) { }
    //check comment nhập trong trạng thái chưa login.
    try { checkComment(); } catch (e) { }
    //check sub-comment nhập trong trạng thái chưa login.
    try { checkSubComment(); } catch (e) { }
    
    //nút Gửi comment cho san pham
    if ($("btnSend")) {
        $("btnSend").onclick = sendCommentDeal;
    }

    try {
        showLisrReferrer();
    } catch (ex) { if ($("divReferrer")) $("divReferrer").style.display = "none"; }
}
//xử lý kiểm tra comment
function checkComment() {
    if (GetCookie("txtCommentDeal") != null && GetCookie("txtCommentDeal") != "") {
        $("txtCommentDeal").value = GetCookie("txtCommentDeal");
    };
}
//xử lý insert comment
function sendCommentDeal() {
    var strUrlLocation = window.location.href;
    strUrlLocation = strUrlLocation.replace('tp-ho-chi-minh/', '').replace('ha-noi/', '');
    var strUrlLink = strPathLink_All.replace('tp-ho-chi-minh/', '').replace('ha-noi/', ''); 
    if (strUrlLocation.toLowerCase().indexOf("/yahoo/") != -1) {
        strUrlLink = strPathLink_All + "yahoo/";
    }
    if (objLogin != null) {
        if ($("txtCommentDeal").value.length > 0) {
            $("btnSend").onclick = "";

            utils.call(strUrlLink + strStateNM + "/nhommua/deal_comments.aspx", { "flag": "insert", "desc": $("txtCommentDeal").value, "id": strDealID }, "POST", "sendCommentDealResult", "sendCommentDealResult");
        }
    } else {
        if ($("txtCommentDeal").value.length > 0)
            SetCookie("txtCommentDeal", $("txtCommentDeal").value, 60);

        window.open(strUrlLink + strStateNM + "/login.aspx", "_self");
    }
}
//xử lý kết quả trả về sau khi insert comment
function sendCommentDealResult(strResult) {
    if (strResult != "0") {
        DeleteCookie("txtCommentDeal", "");
        $("txtCommentDeal").value = "";
        $("btnSend").onclick = sendCommentDeal;
        var strUrlLocation = window.location.href;
        strUrlLocation = strUrlLocation.replace('tp-ho-chi-minh/', '').replace('ha-noi/', '');
        var strUrlLink = strPathLink_All.replace('tp-ho-chi-minh/', '').replace('ha-noi/', ''); 
        if (strUrlLocation.toLowerCase().indexOf("/yahoo/") != -1) {
            strUrlLink = strPathLink_All + "yahoo/";
        }

        utils.call(strUrlLink + strStateNM + "/nhommua/deal_comments.aspx", { "flag": "view", "id": strDealID }, "POST", "showCommentsByDeal", "showCommentsByDeal");

    } else
        $("btnSend").onclick = sendCommentDeal;
}
//xử lý hiển thị comment của sản phẩm
function showCommentsByDeal(objComment) {

    var strHtml = "";
    for (var i = 0; i < objComment.dsComment.length; i++) {

        strHtml += "<div class='comment-content'>";
        strHtml += "<div class='detail-deal'>";
        strHtml += "<p>" + objComment.dsComment[i].noidung + "</p>";
        strHtml += "<p class='user-info'><a href='profile/comments.aspx?mid=" + objComment.dsComment[i].idtk + "' name=''>" + objComment.dsComment[i].nickname + "</a> - " + objComment.dsComment[i].tgtao + "</p>";
        strHtml += "<div class='total-comment' style='cursor: pointer;' onclick='showSubCommentSP(" + objComment.dsComment[i].idcomment + ")' >";
        strHtml += "<span class='icon-comment'></span>";
        strHtml += "<span class='num-comment' id='subCount_" + objComment.dsComment[i].idcomment + "'>" + objComment.dsComment[i].counts + " comments</span>";
        strHtml += "</div>";
        strHtml += "</div>";
        strHtml += "<i></i>";
        strHtml += "<div class='comment-content-right'>";
        strHtml += "<img width='50' height='50' alt='" + objComment.dsComment[i].nickname + "' src='" + objComment.dsComment[i].avatar + "' />";
        strHtml += "</div>";
        strHtml += "<div class='clear'></div>";
        strHtml += "</div>";
        strHtml += "<div id='divSubComment_" + objComment.dsComment[i].idcomment + "' style='display:none'></div>";
    }
    $("ulComment").innerHTML = strHtml;
    if ($("btnSend")) $("btnSend").onclick = sendCommentDeal;
}
//xử lý hiển thị sub-comment cho comment được chọn
var strIDCommentSP = '';
function showSubCommentSP(iIDComment) {
    if ($("divSubComment_" + iIDComment).style.display == "block") {
        $("divSubComment_" + iIDComment).style.display = "none";
    } else {
        strIDCommentSP = iIDComment;
        var strUrlLocation = window.location.href;
        strUrlLocation = strUrlLocation.replace('tp-ho-chi-minh/', '').replace('ha-noi/', '');
        var strUrlLink = strPathLink_All.replace('tp-ho-chi-minh/', '').replace('ha-noi/', ''); 
        if (strUrlLocation.toLowerCase().indexOf("/yahoo/") != -1) {
            strUrlLink = strPathLink_All + "yahoo/";
        }

        utils.call(strUrlLink + strStateNM + "/nhommua/deal_comments.aspx", { "flag": "showsub", "id": iIDComment }, "POST", "showSubCommentSPResult", "showSubCommentSPResult");
    }
}
//xử lý check sub-comment nhập trong trạng thái chưa login
function checkSubComment() {
    if (GetCookie("txtSubComment") != null && GetCookie("txtSubComment") != "") {
        var CommentID = GetCookie("CommentID");
        showSubCommentSP(CommentID);
    };
}
//xử lý kết quả trả về sau khi yêu cầu sub-comment
function showSubCommentSPResult(obj) {
    var strHtml = "";
    
    if (obj.dsSubComment != "0") {
        for (var i = 0; i < obj.dsSubComment.length; i++) {
            strHtml += "<div class='comment-content short-size'>";
            strHtml += "<div class='detail-deal'>";
            strHtml += "<p>" + obj.dsSubComment[i].noidung + "</p>";
            strHtml += "<p class='user-info'><a href='profile/comments.aspx?mid=" + obj.dsSubComment[i].idtk + "' name=''>" + obj.dsSubComment[i].nickname + "</a> - " + obj.dsSubComment[i].tgtao + "</p>";
            strHtml += "</div>";
            strHtml += "<i></i>";
            strHtml += "<div class='comment-content-right'>";
            strHtml += "<img width='50' height='50' src='" + obj.dsSubComment[i].avatar + "'/>";
            strHtml += "</div>";
            strHtml += "<div class='clear'></div>";
            strHtml += "</div>";
        }
        $("subCount_" + obj.IDComm).innerHTML = obj.dsSubComment.length + " comments";
    }
    else
        $("subCount_" + obj.IDComm).innerHTML = "0 comments";

    strHtml += "<div class='comment-content-alignright'>";
    strHtml += "<textarea class='textareaCP' id='txtSubComment_" + obj.IDComm + "' ></textarea>";
    strHtml += "<p><a class='view-button' name='" + obj.IDComm + "' href='javascript:void(0)' onclick='insertSubComment(this.name);' id='btnSub_" + obj.IDComm + "' ></a></p>";
    strHtml += "<div class='clear'></div>";
    strHtml += "</div>";
    $("divSubComment_" + obj.IDComm).style.display = "block";
    $("divSubComment_" + obj.IDComm).innerHTML = strHtml;
    $("txtSubComment_" + obj.IDComm).focus();

    if (GetCookie("txtSubComment") != "undefine" || GetCookie("txtSubComment") != "") {
        var txtSubComment = GetCookie("txtSubComment");
        var txtSubCommentID = GetCookie("txtSubCommentID");
        $(txtSubCommentID).value = txtSubComment;
    };
}
//xử lý insert sub-comment
function insertSubComment(idComment) {
    if ($("txtSubComment_" + idComment).value.length > 0) {
        var strUrlLocation = window.location.href;
        strUrlLocation = strUrlLocation.replace('tp-ho-chi-minh/', '').replace('ha-noi/', '');
        var strUrlLink = strPathLink_All.replace('tp-ho-chi-minh/', '').replace('ha-noi/', ''); 
        if (strUrlLocation.toLowerCase().indexOf("/yahoo/") != -1) {
            strUrlLink = strPathLink_All + "yahoo/";
        }
        if (objLogin != null) {

            DeleteCookie("txtSubComment", "");
            DeleteCookie("txtSubCommentID", "");
            DeleteCookie("CommentID", "");

            var strContent = $("txtSubComment_" + idComment).value;
            $("txtSubComment_" + idComment).value = "";
            strIDCommentSP = idComment;
            utils.call(strUrlLink + strStateNM + "/nhommua/deal_comments.aspx", { "flag": "insertsub", "id": idComment, "idtp": "8", "desc": strContent }, "POST", "showSubCommentSPResult", "showSubCommentSPResult");

        } else {
            if ($("txtSubComment_" + idComment).value.length > 0) {
                SetCookie("txtSubComment", $("txtSubComment_" + idComment).value, 60);
                SetCookie("txtSubCommentID", "txtSubComment_" + idComment, 60);
                SetCookie("CommentID", idComment, 60);
            }
            
            
            window.open(strUrlLink + strStateNM + "/login.aspx", "_self");
        }
    }
}

/********************************************************************************************************/
var iCurrentReferrer = 1;
function showLisrReferrer() {

    var strUrlLocation = window.location.href;
    strUrlLocation = strUrlLocation.replace('tp-ho-chi-minh/', '').replace('ha-noi/', '');
    var strUrlLink = strPathLink_All.replace('tp-ho-chi-minh/', '').replace('ha-noi/', ''); 
    if (strUrlLocation.toLowerCase().indexOf("/yahoo/") != -1) {
        strUrlLink = strPathLink_All + "yahoo/";
    }

    iEnd = 0;
    iStart = 0;
    var iCount = objRefer.dsRefer.length - 1; 
    iTotal = Math.ceil(iCount / 10);
    iStart = iCurrentReferrer * 10 - 10;
    iEnd = iStart + 10;
    iEnd = iEnd > iCount ? iCount : iEnd;

    if (objRefer.dsRefer != "0") {

        var strHTML = "";     

        /*********************************************/
        strHTML += '<div class="title_trans">';
       strHTML += '<h3>Chia sẽ để thêm cơ hội!</h3></div>' +
                               ' <div class="deal_content">' +
                                	'<p style="padding: 5px 0 5px 0; font-size:13px; color:#fff;">Danh sách bạn bè giới thiệu để có thêm cơ hội thắng giải.</p>' +
                                   ' <span style="float:left;padding:15px 4px 0 0; font-size:13px; color:#fff;">- Tổng cộng: </span>' +
                                    '<div class="div_txtSI">' +
                                    '	<input type="text" id="txtMaRefer" value="' + objRefer.dsRefer[iCount].total + '" readonly="false"  />' +
                                  '  </div>' +
                                   ' <span style="float:left;padding:15px 0 0 4px;width:45px; font-size:13px; color:#fff;"> cơ hội</span>' +
                                    '<i class="iLineWinNoki"></i>' +
                                  ' <p style="padding-top:10px; padding-bottom: 5px;font-size:12px; color:#fff;">Chia sẽ <strong>link</strong> dưới để được thêm cơ hội</p>' +
                                   ' <div class="bg-email-deal" style="margin: 0;">' +
                                    	'<span class="bg-email-input" ><input type="text" value="' + strUrlLink + strStateNM + '/?refer=' + objRefer.dsRefer[iCount].id + '&id=' + strDeal.id + '" id="txtLinkReferre" /></span>' +
                                      '  <span class="bg-email-btn" style="text-align:center;" onclick="javascript:selectTextbox()"><em style=" padding-left:0;font-size: 12px;">Chọn</em></span>' +
                                   ' </div> ' +
                                   ' <div class="div_ChiaSe">' +
                                    	'<p style="font-size: 12px;height: 18px;padding: 7px 3px 0 0; float:left; color:#333333;"><strong>Hoặc chia sẻ qua: </strong></p>' +
                                        '<div class="div_iconYahooMail">' +
                                        	'<a href="http://www.facebook.com/sharer.php?u=' + escape(strUrlLink + strStateNM + '/?refer=' + objRefer.dsRefer[iCount].id) + '&id=' + strDeal.id + '" class="aFaceBook" target="_blank"></a>' +
                                       '</div>' +
                                       '<div class="div_iconYahooMail">' +
                                       		'<a href="ymsgr:im?+&msg=' + langRefer()[0][16] + ' - ' + strUrlLink + strStateNM + '/?refer=' + objRefer.dsRefer[iCount].id + '&id=' + strDeal.id + '" class="aYahooChat"></a>' +
                                      '</div>' +
                                    '</div>  ' +
                                    '<div class="list-email">';
                                    	
                                    for (var i = iStart; i < iEnd; i++) {
                                    strHTML += '<p>'+(i + 1) + '. <strong>' + (objRefer.dsRefer[i].email.length > 13 ? objRefer.dsRefer[i].email.substring(0, 13) + "..." : objRefer.dsRefer[i].email) + '</strong> _ MS:<strong>' + objRefer.dsRefer[i].ma + '</strong></p>';
                                }
                                     
                                strHTML+=	'</div>' +
                                    '<div class="small-paging">' +
                                        '<div class="paging-gray-half">' +
                                            '<p>	';
                                                 if (iTotal > 1) {

                                                    if (iCurrentReferrer > 1) {
                                                        strHTML += '<a onclick="pagingRefer(' + (iCurrentReferrer - 1) + ')" href="javascript:void(0)"><</a>';
                                                    }
                                                    for (var i = 0; i < iTotal; i++) {

                                                        strHTML += '<a onclick="pagingRefer(' + (i + 1) + ')" style="' + (iCurrentReferrer == (i + 1) ? 'color:#00ccff' : '') + '" href="javascript:void(0)">' + (i + 1) + '</a>';

                                                    }
                                                    if (iCurrentReferrer < iTotal) {
                                                        strHTML += '<a onclick="pagingRefer(' + (iCurrentReferrer + 1) + ')" href="javascript:void(0)">></a>';
                                                    }
                                                }
                                         strHTML+=   '</p>   '+                                  
                                        '</div>'+
                                    '</div>'+
                                '</div>';                                         
        /************************************************/        
        $("divReferrer").innerHTML = strHTML;
        $("divReferrer").style.display = "block";
    }
    else {
       
        $("divReferrer").style.display = "none";
    }
	
}
/******************************************************************************************/
function pagingRefer(ipage) {
    var iCount = objRefer.dsRefer.length - 1;
    iTotal = Math.ceil(iCount / 10);
    iCurrentReferrer = ipage <= 0 ? 1 : (ipage > iTotal ? iTotal : ipage);
    showLisrReferrer();
}

function selectTextbox() {
    $("txtLinkReferre").select();
}